package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.yy.common.base.BaseActivity;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.bean.JGOrderInfo;
import com.yy.jhl.interfaces.CommonCallBack;
import com.yy.jhl.interfaces.DoneOrderCallBack;
import com.yy.jhl.ui.manager.adapter.JGOrderDetailAdapter;
import com.yy.jhl.ui.manager.contract.JGOrderListContract;
import com.yy.jhl.ui.manager.contract.MenuOneContract;
import com.yy.jhl.ui.manager.presenter.JGOrderListPresenter;
import com.yy.jhl.util.DataRequestUtil;
import com.yy.jhl.util.DialogUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 监管订单详情
 * Created by libingn on 2017/12/18.
 */

public class JGOrderActivity extends BaseActivity<JGOrderListPresenter> implements JGOrderListContract.View,PullToRefreshBase.OnRefreshListener2 {
    TextView orderNameText;
    TextView orderCodeText;
    TextView orderDoingText;
    TextView orderDoneText;
    TextView manager_text;
    ListView listView;
    JGOrderDetailAdapter jgOrderDetailAdapter;
    @BindView(R.id.order_pull_list_view)
    PullToRefreshListView orderPullListView;
    private int currentPage = 0;
    private String projectId = "";
    View headerView;
    public static void startAction(Context mContext,String doingOrder,String doneOrder,String projectId) {
        Intent intent = new Intent(mContext, JGOrderActivity.class);
        intent.putExtra("doingOrder",doingOrder);
        intent.putExtra("doneOrder",doneOrder);
        intent.putExtra("projectId",projectId);
        mContext.startActivity(intent);
    }

    private void addHeadView() {
        headerView = LayoutInflater.from(mContext).inflate(R.layout.header_act_jg_order, null);
        orderNameText = (TextView) headerView.findViewById(R.id.order_name_text);
        orderCodeText = (TextView) headerView.findViewById(R.id.order_code_text);
        orderDoingText = (TextView) headerView.findViewById(R.id.order_doing_text);
        orderDoneText = (TextView) headerView.findViewById(R.id.order_done_text);
        manager_text = (TextView) headerView.findViewById(R.id.manager_text);
        listView.addHeaderView(headerView);
        orderDoingText.setText(getIntent().getStringExtra("doingOrder"));
        orderDoneText.setText(getIntent().getStringExtra("doneOrder"));
        manager_text.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                //完结
                String title = "是否确定完结该项目？";
                if (Integer.parseInt(getIntent().getStringExtra("doingOrder")) == Integer.parseInt(getIntent().getStringExtra("doingOrder"))){
                    title = "您还有未完成的订单，是否强制完结？";
                }else {

                }
                DialogUtil.showEnsureDialogView(mContext, title, new CommonCallBack() {
                    @Override
                    public void getCallBack() {
                        DataRequestUtil.getInstance(mContext).cgsDoneOrder(projectId, "2", 0, new DoneOrderCallBack() {
                            @Override
                            public void getCallBack(int position) {

                            }
                        });
                    }
                });
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_jg_order;
    }

    @Override
    public void initView() {
        projectId = getIntent().getStringExtra("projectId");
        orderPullListView.setMode(PullToRefreshBase.Mode.BOTH);
        listView = orderPullListView.getRefreshableView();
        listView.setDividerHeight(3);
        orderPullListView.setOnRefreshListener(this);
        addHeadView();
        showTitle("监管订单", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        jgOrderDetailAdapter = new JGOrderDetailAdapter(mContext);
        listView.setAdapter(jgOrderDetailAdapter);
        mPresenter.getOrderList(1,0,currentPage,projectId,true);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        jgOrderDetailAdapter.clearData();
        currentPage = 0;
        mPresenter.getOrderList(1,0,currentPage,projectId,false);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        currentPage ++;
        mPresenter.getOrderList(1,0,currentPage,projectId,false);
    }

    @Override
    public void getOrderListSuccess(List<JGOrderInfo> orderInfos) {
        orderPullListView.onRefreshComplete();
        if (orderInfos != null && orderInfos.size() > 0){
            JGOrderInfo firstItem = orderInfos.get(0);
            orderNameText.setText(firstItem.getProject_name());
            orderCodeText.setText(firstItem.getOrder_sn());
            jgOrderDetailAdapter.addData(orderInfos);
        }
    }

    @Override
    public void getOrderListFail() {
        orderPullListView.onRefreshComplete();
    }
}
